<?php

namespace App\Modules\Admin\Http\Controllers;

use App\Http\Controllers\BaseController;
use App\Services\VideoService;
use Illuminate\Http\Request;

class VideoController extends BaseController
{
    public function __construct(VideoService $videoService)
    {
        $this->middleware('check.adminLogin')->except('index');

        $this->videoService = $videoService;
    }

    public function index()
    {
        $param = $this->validationParameter([
            'page|页码' => 'noRequire|number|default:1',
            'per_page|显示数量' => 'noRequire|number|default:30',
            'type|类型' => 'noRequire|number',
        ]);

        $data = $this->videoService->getList($param);

        return $this->response->success($data);
    }

    public function show(Request $request)
    {
        $id = $request->input('id');
        $result = $this->videoService->getInfo($id);

        return $this->response->success($result);
    }

    public function store(Request $request)
    {
        $param = $this->validationParameter([
            'title|标题' => 'require',
            'url|播放url' => 'require',
            'duration|时长' => 'require',
            'cover|封面' => 'require',
            'type|类型' => 'require|number|default:1',
            'desc|简介' => 'noRequire',
            'content|内容' => 'noRequire',
        ]);

        $param['publisher_uid'] = $request->user()->admin_uid ?? null;

        $result = $this->videoService->add($param);

        return $this->response->success($result);
    }

    public function update(Request $request)
    {
        $id = $request->input('id');
        $param = $this->validationParameter([
            'title|标题' => 'require',
            'url|播放url' => 'require',
            'duration|时长' => 'require',
            'cover|封面' => 'require',
            'type|类型' => 'require|number|default:1',
            'desc|简介' => 'noRequire',
            'content|内容' => 'noRequire',
        ]);

        $param['publisher_uid'] = $request->user()->admin_uid ?? null;

        $result = $this->videoService->update($param, $id);

        return $this->response->success($result);
    }

    public function destroy(Request $request)
    {
        $id = $request->input('id');
        $result = $this->videoService->delete($id);

        return $this->response->success($result);
    }
}
